/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.goods.entity.BizGoodsCheck;
import vip.xiaonuo.biz.modular.goods.entity.BizGoodsLedger;
import vip.xiaonuo.biz.modular.goods.entity.BizGoodsLedgerCheck;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsCheckMapper;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsLedgerCheckMapper;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsLedgerMapper;
import vip.xiaonuo.biz.modular.goods.param.ClientGoodsCheckParam;
import vip.xiaonuo.biz.modular.goods.result.ClientGoodsCheckItemResult;
import vip.xiaonuo.biz.modular.goods.result.ClientGoodsCheckResult;
import vip.xiaonuo.biz.modular.goods.service.BizGoodsCheckService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author kongxm
 * @date 2022/11/18 13:44
 **/
@Service
@Transactional
public class BizGoodsCheckServiceImpl extends ServiceImpl<BizGoodsCheckMapper, BizGoodsCheck> implements BizGoodsCheckService {
    @Resource
    private BizGoodsLedgerCheckMapper bizGoodsLedgerCheckMapper;
    @Resource
    private BizGoodsLedgerMapper bizGoodsLedgerMapper;

    @Override
    public List<ClientGoodsCheckItemResult> listClientGoodsCheckItems(ClientGoodsCheckParam param) {
        BizGoodsLedger bizGoodsLedger = bizGoodsLedgerMapper.selectById(param.getLedgerId());

        //如果已经有记录，返回检查项结果表
        List<ClientGoodsCheckItemResult> resultList = getClientGoodsCheckItemResults(param.getLedgerId(), param.getCheckType());
        if (CollectionUtil.isNotEmpty(resultList)) return resultList;

        //否则根据物品的检查项查找
        LambdaQueryWrapper<BizGoodsCheck> query = new LambdaQueryWrapper<BizGoodsCheck>()
                .eq(BizGoodsCheck::getGoodsId, bizGoodsLedger.getGoodsId());
        if (param.getCheckType() == 0) {
            query.eq(BizGoodsCheck::getBorrowCheck, 1);
        } else if (param.getCheckType() == 1) {
            query.eq(BizGoodsCheck::getReturnInsp, 1);
        }
        return getBaseMapper().selectList(query).stream().map(item -> {
            ClientGoodsCheckItemResult checkItem = new ClientGoodsCheckItemResult();
            BeanUtil.copyProperties(item, checkItem);
            checkItem.setCheckId(item.getId());
            return checkItem;
        }).collect(Collectors.toList());
    }

    @NotNull
    private List<ClientGoodsCheckItemResult> getClientGoodsCheckItemResults(String ledgerId, Integer checkType) {
        return bizGoodsLedgerCheckMapper.selectList(new LambdaQueryWrapper<BizGoodsLedgerCheck>()
                .eq(BizGoodsLedgerCheck::getLedgerId, ledgerId)
                .eq(BizGoodsLedgerCheck::getCheckType, checkType)
                .eq(BizGoodsLedgerCheck::getDeleteFlag, "NOT_DELETE")
        ).stream().map(item -> BeanUtil.copyProperties(item, ClientGoodsCheckItemResult.class))
                .collect(Collectors.toList());
    }
}
